<template>
  <div>
    <transition
      name="custom-classes-transition"
      enter-active-class="animated fadeIn"
      leave-active-class="animated fadeOut"
    >
      <div class="modal" v-show="isOpen" @click="isOpen = false">
        <div class="modal-dialog" @click.stop>
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close"  @click="isOpen = false"><span>&times;</span></button>
              <h4 class="modal-title">添加数据库</h4>
            </div>
            <div class="modal-body">
              <div class="form-group">
                <p>输入ES数据库的REST服务的地址</p>
                <p>例如：<code>http://localhost:9200</code></p>
                <div class="input-group">
                  <span class="input-group-addon" id="basic-addon1">http://</span>
                  <input type="text" class="form-control" placeholder="localhost:9200" v-model="url">
                </div>
              </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" @click="isOpen = false">取消</button>
              <button type="button" class="btn btn-primary" @click="handelConfirm">确定</button>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <transition
      name="custom-classes-transition"
      enter-active-class="animated fadeIn"
      leave-active-class="animated fadeOut"
    >
      <div ><div class="modal-backdrop" v-show="isOpen"></div></div>
    </transition>
  </div>
</template>

<script>
import localforage from 'localforage'

export default {
  data () {
    return {
      isOpen: false,
      url: 'localhost:9200'
    }
  },
  methods: {
    open () {
      this.isOpen = true
    },
    close () {
      this.isOpen = false
    },
    handelConfirm () {
      localforage.getItem('urls').then((value) => {
        var urls = value || []
        urls.push(this.url)
        return localforage.setItem('urls', urls)
      }).then(() => {
        this.$emit('confirm')
        this.close()
      })
    }
  }
}
</script>

<style>

</style>
